package com.yjd.comm.util;

import com.yjd.comm.annotation.ServiceRemoteProxyHandle;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import org.apache.log4j.Logger;

/**
 * <b>description</b>：service服务获取工具 <br>
 * <b>time</b>：2014-8-22 下午7:14:54 <br>
 * <b>author</b>： ready likun_557@163.com
 */
public class ServiceHolder {
	private static Logger logger = Logger.getLogger(ServiceHolder.class);

	/**
	 * 获取service对象
	 * 
	 * @param serviceClass
	 * @param serviceName
	 * @return
	 */
	public static <T> T getService(Class<T> serviceClass, String serviceName) {
		T t = getService(serviceClass, serviceName, RpmServiceKeyEnum.RPM_PUBLIC_KEY_E);
		return t;
	}
	
	/**
	* 获取service对象
	* @param serviceClass
	* @param serviceName
	* @param rpmServiceKeyEnum
	* @return
	 */
	public static <T> T getService(Class<T> serviceClass, String serviceName,RpmServiceKeyEnum rpmServiceKeyEnum) {
	    return getService(serviceClass, serviceName, rpmServiceKeyEnum, false);
	}
	
	/**
	* 获取service对象
	* @param serviceClass
	* @param serviceName
	* @param rpmServiceKeyEnum
	* @param flag 是否直接过去远程对象
	* @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getService(Class<T> serviceClass, String serviceName,RpmServiceKeyEnum rpmServiceKeyEnum,boolean flag) {
		Object object = null;
		if(!flag){
        	    try {
        		object = SpringContextHolder.getBean(serviceName);
        		if (object != null) {
        		    return (T) object;
        		}
        	    } catch (Exception e) {
        	    }
		}
		try {
			return (T) ServiceRemoteProxyHandle.getServiceProxy(serviceClass,
					rpmServiceKeyEnum.getRpmServiceUrl() + "/" + serviceName);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return null;
	}

	public static <T> T getLocalService(Class<T> serviceClass, String serviceName) {
		T t = getService(serviceClass, serviceName,null,false);
		return t;
	}

}